package com.winter.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.winter.mapper.CustomerMessageMapper;
import com.winter.model.CustomerMessage;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
public class CustomerMessageService {
    
	@Autowired
	public CustomerMessageMapper mapper;
	
	/**
	 * 获取留言管理列表
	 * @param pageNumber
	 * @param pageSize
	 * @param
	 * @return
	 */
	public Result queryPage(Integer pageNumber, Integer pageSize, String createUser,Long id) {
		if (Strings.isNullOrEmpty(createUser)) {
			return Result.success("创建人不能为空");
		}
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
		PageHelper.startPage(pageNumber, pageSize, false);
		List<CustomerMessage> list = mapper.queryPage(createUser,id);
		PageInfo<CustomerMessage> pageInfo = new PageInfo<>(list);
		return Result.success(pageInfo);
	}
	
	/**
	 * 添加留言
	 * @param message
	 * @return
	 */
	public Result insert(CustomerMessage message){
		if (Strings.isNullOrEmpty(message.getType())) {
			if (message.getType().equals("1")) {
				if (Strings.isNullOrEmpty(message.getCustName())) {
					return Result.success("客户姓名不能为空");
				}
				if (Strings.isNullOrEmpty(message.getRevisitType())) {
					return Result.success("请选择回访类型");
				}
				if (Strings.isNullOrEmpty(message.getCustEmail())) {
					return Result.success("联系邮箱不能为空");
				}
				if (Strings.isNullOrEmpty(message.getCustPhone())) {
					return Result.success("手机号不能为空");
				}
			}else if (message.getType().equals("2")){
				if (Strings.isNullOrEmpty(message.getAffix())) {
					return Result.success("图片不能为空");
				}
			} 
		}
		if (Strings.isNullOrEmpty(message.getMessageType())) {
			return Result.success("请选择留言类型");
		}
		if (Strings.isNullOrEmpty(message.getCreateUser())) {
			return Result.success("创建用户不能为空");
		}
		if (Strings.isNullOrEmpty(message.getMessageContent())) {
			return Result.success("内容不能为空");
		}
		message.setIsvalid("1");
		message.setCreatetime(new Date());
		int i = mapper.insertSelective(message);
	    if (i <= 0) {
	        return Result.failure(ResultCode.FAILURE, "添加留言失败");
	    }
		return Result.success();
	}
}
